package com.gsoe.auth.controller;

import cn.dev33.satoken.stp.SaLoginModel;
import cn.dev33.satoken.stp.StpUtil;
import com.gsoe.auth.dto.UpdateUserDTO;
import com.gsoe.auth.dto.UserProblemCollectDTO;
import com.gsoe.auth.vo.CodeVO;
import com.gsoe.auth.dto.UserRegisterDTO;
import com.gsoe.auth.vo.UserInfoVO;
import com.gsoe.auth.vo.UserVO;
import com.gsoe.auth.service.UserInfoService;
import com.gsoe.auth.service.UserRankingService;
import com.gsoe.base.exception.UserException;
import com.gsoe.base.model.PageResult;
import com.gsoe.base.model.Result;
import com.gsoe.base.model.User;
import com.gsoe.auth.dto.UserLoginDTO;
import com.gsoe.auth.vo.UserProblemCollectVO;
import jakarta.validation.Valid;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class UserController {
    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private UserRankingService userRankingService;

    @PostMapping("/doRegister")

    public Result<String> doRegister(@RequestBody @Valid UserRegisterDTO userRegisterDTO) {
        userInfoService.doRegister(userRegisterDTO);
        return Result.success("注册成功");
    }

    @GetMapping("/getEmailCode")
    public Result<String> getEmailCode(String email) {
        userInfoService.getEmailCode(email);
        return Result.success("ok");
    }

    @GetMapping("/getUserInfo/{id}")
    public Result<UserInfoVO> getUserInfo(@PathVariable Long id) {
        return Result.success(userInfoService.getUserInfo(id));
    }


    @PostMapping("/doLogin")
    public Result<UserVO> doLogin(@RequestBody @Valid UserLoginDTO userLoginDTO) {
        User user=userInfoService.doLogin(userLoginDTO);
        StpUtil.login(user.getId(),new SaLoginModel()
                .setExtra("info", user)
        );
        UserVO userVO=new UserVO();
        BeanUtils.copyProperties(user,userVO);
        return Result.success(userVO);
    }
    @GetMapping("/getCodeImage")
    public Result<CodeVO> getCodeImage() {
        return  Result.success(userInfoService.getCodeImage());
    }

    @PostMapping("/getRoleList")
    public List<String> getRoleList(@RequestBody Long userId) {
        return userInfoService.getRoleList(userId);
    }

    @PostMapping("/getPowerListByRoleName")
    public Result<List<String>> getPowerListByRoleName(@RequestBody String roleName) {
        return Result.success(userInfoService.getPowerListByRoleName(roleName));
    }

    @GetMapping("/check")
    public Result<String> check() {
        boolean b = StpUtil.hasPermission("access_admin_view");
        if(!b)throw new UserException("无访问管理界面权限");
        return Result.success("ok");
    }
    //我的收藏---查
    @GetMapping("/getUserProblemCollect/{pageNo}/{pageSize}")
    public Result<PageResult<UserProblemCollectVO>>  getUserProblemCollect(@PathVariable(value = "pageNo")Integer pageNo, @PathVariable(value = "pageSize")Integer pageSize){
        return Result.success(userInfoService.getUserProblemCollect(pageNo,pageSize));
    }

    @PostMapping("/addUserProblemCollect")
    public Result<String> addUserProblemCollect(@RequestBody UserProblemCollectDTO userProblemCollectDTO){
        userInfoService.addUserProblemCollect(userProblemCollectDTO);
        return Result.success("ok");
    }

    @DeleteMapping("/deleteUserProblemCollect/{problemId}")
    public Result<String> deleteUserProblemCollect(@PathVariable Long problemId){
        userInfoService.deleteUserProblemCollect(problemId);
        return Result.success("ok");
    }

    @GetMapping("/getUserAllProblemCollect/{userId}")
    Result<List<Long>> getUserAllProblemCollect(@PathVariable("userId") Long id){
        return Result.success(userInfoService.getUserAllProblemCollect(id));
    }
    @PutMapping("/updateUserInfo")
    public Result<String> updateUserInfo(@RequestBody @Valid UpdateUserDTO updateUserDTO){
        userInfoService.updateUserInfo(updateUserDTO);
        return Result.success("ok");
    }
}
